package view;

import java.awt.BorderLayout;
import java.awt.EventQueue;
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;

import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;
import javax.swing.JTextPane;   
import javax.swing.JToggleButton;
import javax.swing.SpringLayout;
import javax.swing.UIManager;
import javax.swing.UnsupportedLookAndFeelException;
import javax.swing.border.EmptyBorder;

import control.*;
import java.awt.event.WindowAdapter;
import java.awt.event.WindowEvent;
import service.*;
//import sun.org.mozilla.javascript.internal.ObjToIntMap;
import java.awt.event.ActionListener;
import javax.swing.text.BadLocationException;
import javax.swing.text.Document;

public class AdminView extends JFrame {

	private ServerController server=null;
	private JPanel contentPane;
	private JTextField textField;
	private JPanel mainPanel;
	private JLabel onlinelbl;
	private JLabel offlinelbl;
	private JTextPane textPane;
	private JButton btnAnnounce;
	private JLabel lblLastAnnouncements;
	private JLabel lblOnline;
	private JLabel lblOffline;
	private JToggleButton btnNewButton;
	private boolean annouce;
        private ChatClientService chatClientService;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					AdminView frame = new AdminView();
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the frame.
	 */
	public AdminView() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		try {
			UIManager.setLookAndFeel("de.javasoft.plaf.synthetica.SyntheticaAluOxideLookAndFeel");
		} catch (ClassNotFoundException | InstantiationException
				| IllegalAccessException | UnsupportedLookAndFeelException e) {
			e.printStackTrace();
		}
		setBounds(100, 100, 450, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		contentPane.setLayout(new BorderLayout(0, 0));
		setContentPane(contentPane);
		
		JPanel panel = new JPanel();
		((FlowLayout)panel.getLayout()).setAlignment(FlowLayout.LEFT);
		contentPane.add(panel, BorderLayout.NORTH);
                JButton DataAdmin=new JButton("DataBase Admin");
                 DataAdmin.addActionListener(new ActionListener() {

                @Override
                public void actionPerformed(ActionEvent ae) {
//   throw new UnsupportedOperationException("Not supported yet.");
                    DisplayQueryResults displ=new DisplayQueryResults();
                    
                    
                }
                 } );
		panel.add(DataAdmin);
		
		btnNewButton = new JToggleButton("Start Server");
		
		btnNewButton.addItemListener(new ItemListener() {
			public void itemStateChanged(ItemEvent ev) {
				if (ev.getStateChange() == ItemEvent.SELECTED) {
					setCompEnabled(true);
					btnNewButton.setText("Stop Server");
                                        server.startServer();                                        
				} else if (ev.getStateChange() == ItemEvent.DESELECTED) {
					setCompEnabled(false);
					btnNewButton.setText("Start Server");
                                        server.propagateCloseAction();
                                        server.stopServer();
				}
			}
		});
		panel.add(btnNewButton);
		
		mainPanel = new JPanel();
		contentPane.add(mainPanel, BorderLayout.CENTER);
		SpringLayout sl_mainPanel = new SpringLayout();
		mainPanel.setLayout(sl_mainPanel);
		
		textField = new JTextField();
		sl_mainPanel.putConstraint(SpringLayout.WEST, textField, 10, SpringLayout.WEST, mainPanel);
		sl_mainPanel.putConstraint(SpringLayout.EAST, textField, -127, SpringLayout.EAST, mainPanel);
		mainPanel.add(textField);
		textField.setColumns(10);
		
		btnAnnounce = new JButton("Announce");
		sl_mainPanel.putConstraint(SpringLayout.WEST, btnAnnounce, 6, SpringLayout.EAST, textField);
		sl_mainPanel.putConstraint(SpringLayout.EAST, btnAnnounce, -10, SpringLayout.EAST, mainPanel);
		sl_mainPanel.putConstraint(SpringLayout.NORTH, textField, -22, SpringLayout.SOUTH, btnAnnounce);
		sl_mainPanel.putConstraint(SpringLayout.SOUTH, textField, 0, SpringLayout.SOUTH, btnAnnounce);
		sl_mainPanel.putConstraint(SpringLayout.SOUTH, btnAnnounce, -11, SpringLayout.SOUTH, mainPanel);
		mainPanel.add(btnAnnounce);
                
                btnAnnounce.addActionListener(new ActionListener (){
                @Override
                  public void actionPerformed(ActionEvent e) {
                try {
                    Document doc = textPane.getDocument();
                    doc.insertString(doc.getLength(), "\n - "+getAnnounceMessage(), null);
                    //textPane.setText(getAnnounceMessage());
                    server.addAnnouncements(getAnnounceMessage());
                } catch (BadLocationException ex) {
                    ex.printStackTrace();
                }
                    
                  }
                });

                
		lblLastAnnouncements = new JLabel("Last Announcements:");
		sl_mainPanel.putConstraint(SpringLayout.NORTH, lblLastAnnouncements, 10, SpringLayout.NORTH, mainPanel);
		sl_mainPanel.putConstraint(SpringLayout.WEST, lblLastAnnouncements, 10, SpringLayout.WEST, mainPanel);
		mainPanel.add(lblLastAnnouncements);
		
		JScrollPane scrollPane = new JScrollPane();
		sl_mainPanel.putConstraint(SpringLayout.NORTH, scrollPane, 12, SpringLayout.SOUTH, lblLastAnnouncements);
		sl_mainPanel.putConstraint(SpringLayout.WEST, scrollPane, 10, SpringLayout.WEST, mainPanel);
		sl_mainPanel.putConstraint(SpringLayout.SOUTH, scrollPane, -3, SpringLayout.NORTH, textField);
		sl_mainPanel.putConstraint(SpringLayout.EAST, scrollPane, -127, SpringLayout.EAST, mainPanel);
		mainPanel.add(scrollPane);
		
		textPane = new JTextPane();
		textPane.setEditable(false);
		scrollPane.setViewportView(textPane);
		
		lblOnline = new JLabel();
		lblOnline.setIcon(new ImageIcon(AdminView.class.getResource("/resources/online-icon.png")));
		sl_mainPanel.putConstraint(SpringLayout.NORTH, lblOnline, 36, SpringLayout.NORTH, mainPanel);
		sl_mainPanel.putConstraint(SpringLayout.WEST, lblOnline, 6, SpringLayout.EAST, scrollPane);
		sl_mainPanel.putConstraint(SpringLayout.EAST, lblOnline, -80, SpringLayout.EAST, mainPanel);
		mainPanel.add(lblOnline);
		
		onlinelbl = new JLabel("New label");
		sl_mainPanel.putConstraint(SpringLayout.NORTH, onlinelbl, 0, SpringLayout.NORTH, scrollPane);
		mainPanel.add(onlinelbl);
		
		lblOffline = new JLabel();
		lblOffline.setIcon(new ImageIcon(AdminView.class.getResource("/resources/offline-icon.png")));
		sl_mainPanel.putConstraint(SpringLayout.NORTH, lblOffline, 4, SpringLayout.SOUTH, lblOnline);
		sl_mainPanel.putConstraint(SpringLayout.WEST, lblOffline, 0, SpringLayout.WEST, btnAnnounce);
		mainPanel.add(lblOffline);
		
		offlinelbl = new JLabel("New label");
		sl_mainPanel.putConstraint(SpringLayout.WEST, onlinelbl, 0, SpringLayout.WEST, offlinelbl);
		sl_mainPanel.putConstraint(SpringLayout.NORTH, offlinelbl, 0, SpringLayout.NORTH, lblOffline);
		sl_mainPanel.putConstraint(SpringLayout.WEST, offlinelbl, 21, SpringLayout.EAST, lblOffline);
		mainPanel.add(offlinelbl);
		setCompEnabled(false);
                this.addWindowListener(new FrameCloseListener());
	}

    /**
     * @return the chatClientService
     */
    public ChatClientService getChatClientService() {
        return chatClientService;
    }

    /**
     * @param chatClientService the chatClientService to set
     */
    public void setChatClientService(ChatClientService chatClientService) {
        this.chatClientService = chatClientService;
    }
        class FrameCloseListener extends WindowAdapter {

        @Override
        public void windowClosing(WindowEvent e) {
            server.propagateCloseAction();

        }
    }
	
	private void setCompEnabled(boolean enabled) {
		textField.setEnabled(enabled);
		btnAnnounce.setEnabled(enabled);
		textPane.setEnabled(enabled);
		lblLastAnnouncements.setEnabled(enabled);
		onlinelbl.setEnabled(enabled);
		offlinelbl.setEnabled(enabled);
		lblOnline.setEnabled(enabled);
		lblOffline.setEnabled(enabled);
		if(!enabled) {
			onlinelbl.setText("");
			offlinelbl.setText("");
		} else {
			onlinelbl.setText("0");
			offlinelbl.setText("0");
			textField.requestFocus();
		}
	}
	
	public synchronized void updateOnline(int login) {
            onlinelbl.setText(String.valueOf(login));
	}
        
        public int getOnline() {
            return Integer.parseInt(onlinelbl.getText());
        }
	
	public void updateOffline(int loggedoff) {
		offlinelbl.setText(String.valueOf(loggedoff));
	}
        
        public int getOffline() {
            return Integer.parseInt(offlinelbl.getText());
        }
	
	public boolean isAnnouce() {
		return annouce;
	}
	
	public void setAnnouce(boolean annouce) {
		this.annouce = annouce;
	}
	
	public String getAnnounceMessage() {
		return textField.getText();
	}
	
	public void setServer(ServerController server) {
		this.server = server;
	}	
}
